function swap(arr, i, j) {
	var temp = arr[i];
	arr[i] = arr[j];
	arr[j] = temp;
}

//5,8
function randomInt(min, max) {
	return parseInt(Math.random() * (max - min + 1)) + min;
}

//生成随机颜色
function randomColor(){
	return  `rgb(${randomInt(0,255)},${randomInt(0,255)},${randomInt(0,255)})`;
}

//判断当前日期是否为闰年
function isLeapYear() {
	var now = new Date();
	var y = now.getFullYear();
	return y % 400 == 0 || y % 4 == 0 && y % 100 != 0;
}

function date2string(d) {
	if (!d) {
		console.error("参数写错了，请传入日期对象");
		return;
	}
	if (typeof d == "object" && d instanceof Date) {
		var ymd = d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();
		var hms = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
		return ymd + " " + hms;
	} else {
		console.error("参数写错了，请传入日期对象");
		return;
	}
}

//计算两个日期的差值
function between(d1, d2) {
	if (typeof d1 == "object" && d1 instanceof Date) {
		var time1 = d1.getTime();
	} else if (typeof d1 == "string") {
		var time1 = Date.parse(d1);
	}

	if (typeof d2 == "object" && d2 instanceof Date) {
		var time2 = d2.getTime();
	} else if (typeof d2 == "string") {
		var time2 = Date.parse(d2);
	}
	var days = Math.abs(time1 - time2) / 1000 / 60 / 60 / 24;
	return days;

}

//获得N天以后的日期
function getNDaysAfter(n) {
	var now = new Date();
	now.setDate(now.getDate() + n);
	return now;
}

//从节点列表中过滤文本节点，返回元素节点
function eleNodeFilter(nodelist) {
	var arr = [];
	for (var i = 0; i < nodelist.length; i++) {
		if (nodelist[i].nodeType == 1) {
			arr.push(nodelist[i]);
		}
	}
	return arr;
}


function getStyle(ele) {
	return ele.currentStyle ? ele.currentStyle : getComputedStyle(ele);
}

function getPagePosition(ele, option) {

	/* if(ele.offsetParent == null) return {
		x : ele.offsetLeft,
		y : ele.offsetTop
	}
	
	return {
		x : ele.offsetLeft + getPagePosition(ele.offsetParent).x,
		y : ele.offsetTop + getPagePosition(ele.offsetParent).y
	}; */


	var pos = {
		x: option == "center" ? ele.offsetWidth / 2 : 0,
		y: option == "center" ? ele.offsetHeight / 2 : 0
	};
	while (ele) {
		pos.x += ele.offsetLeft;
		pos.y += ele.offsetTop;
		ele = ele.offsetParent;
	}
	return pos;
}


function addEvent(ele, eventtype, fn) {
	if (ele.attachEvent) {
		ele.attachEvent("on" + eventtype, fn);
	} else {
		ele.addEventListener(eventtype, fn);
	}
}

function $(selector) {
	if (typeof selector != "string") return;
	if (selector.length == 0) return;

	var first = selector[0];
	if (first == "#") {
		return document.getElementById(selector.substring(1))
	} else if (first == ".") {
		return Array.from(document.getElementsByClassName(selector.substring(1)))
	} else {
		return Array.from(document.getElementsByTagName(selector));
	}
}

/** 
 * 编写一个拖拽函数封装，要求如下：
 * 	drag(ele, {
		y : false   //表示元素不可以垂直拖拽
		x : true,    //表示元素可以水平拖拽
		limit : true,   //表示元素只能在参考系的范围内容移动
		draging : function(offsetX,offsetY){
			//该函数会在元素拖拽过程中被不断执行
			//所传入的参数，offsetX,offsetY代表元素相对于参考系的坐标
		}
	})
 */

 

function $$(selector) {
	var startWidth = null;
	var hasAttr = false;
	if (/^#/.test(selector)) startWidth = "id";
	if (/^\./.test(selector)) startWidth = "class";
	if (/^[a-z]/i.test(selector)) startWidth = "tag";

	var list1 = selector.match(/.+\[(.+)=.+\]/);
	var attrname = list1 ? list1[1] : null;
	var list2 = selector.match(/.+\[.+=(.+)\]/);
	var attrvalue = list2 ? list2[1] : null;
	switch (startWidth) {
		case "id": {
			return _id(selector.substring(1));
		}
		case "class": {
			//.page[xxx=xx]
			var classname = selector.match(/\.([\w-]+)/)[1];
			var list = _class(classname);
			if (attrname == null) {
				return list;
			} else {
				return list.filter(function(item) {
					return (item[attrname] == attrvalue) || (item.getAttribute(attrname) == attrvalue);
				});
			}
		}
		case "tag": {
			var tagname = selector.match(/^([a-zA-Z0-9]+)/)[1];
			var list = _tag(tagname);
			if (attrname == null) {
				return list;
			} else {
				return list.filter(function(item) {
					return (item[attrname] == attrvalue) || (item.getAttribute(attrname) == attrvalue);
				})
			}
		}
	}

	function _id(id) {
		return document.getElementById(id);
	}

	function _class(classname) {
		return Array.from(document.getElementsByClassName(classname));
	}

	function _tag(tagname) {
		return Array.from(document.getElementsByTagName(tagname));
	}
}

//动画缓冲
function animate(ele, options, callback) {
	stop();

	for (let attr in options) {
		let targetValue = options[attr];

		let startValue = attr == "opacity" ? getComputedStyle(ele)["opacity"] * 100 : parseInt(getComputedStyle(ele)[attr]);
		let distance = targetValue - startValue;

		let degree = 0;

		ele[attr + "-timer"] = setInterval(() => {
			degree += 3;
			if (attr == "opacity") ele.style[attr] = (startValue + Math.sin(degree * Math.PI / 180) * distance) / 100;
			else ele.style[attr] = startValue + Math.sin(degree * Math.PI / 180) * distance + "px";
			if (degree == 90) {
				clearInterval(ele[attr + "-timer"]);
				callback ? callback() : "";
			}
		}, 30)
	}

	function stop() {
		for (let attr in options) {
			clearInterval(ele[attr + "-timer"]);
		}
	}
}
function getCookie(key) {
	var kvlist = document.cookie.split("; ");
	var kv = kvlist.find(function(item) {
		return item.split("=")[0] == key;
	});

	if (kv) {
		return kv.split("=")[1];
	}
}

function setCookie(key, val, days) {
	document.cookie = key + "=" + val + "; expires=" + getNDaysAfter(days) + "; path= /"
}


